import { setPermissionCodes, getPermissionCodes } from "@/store/localStorage/index.js"
/**
 * 根据菜单数据获取权限代码
 * @param {*} data 菜单数据
 */
export const collectPermissionCodes = (data) => {
    let permissionCodes = []
    data.forEach(element => {
      if (element.permission && element.permission.length) {
        element.permission.forEach(item => {
          item.code && permissionCodes.push(item.code)
        })
      }
      if (element.child && element.child.length) {
        element.child.forEach(item => {
          if (item.permission && item.permission.length) {
            item.permission.forEach(p => {
              p.code && permissionCodes.push(p.code)
            })
          }
        })
      }
    })
    setPermissionCodes(permissionCodes)
  }
  /**
   * 判断是否有权限
   */
  export const isHavePermission = (code) => {
    const allPermission = getPermissionCodes()
    if (allPermission.indexOf(code) > -1) {
      return true
    } else {
      return false
    }
  }